<template>
  <div class="role-management">
    <!-- 搜索和添加区域 -->
    <div class="operation-area">
      <el-button type="primary" @click="showAddDialog">添加角色+</el-button>
      <div class="search-area">
        <el-input
          v-model="searchParams.roleName"
          placeholder="请输入角色名称"
          clearable
          style="width: 200px; margin-right: 10px"
        />
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </div>
    </div>

    <!-- 角色表格 -->
    <el-table :data="pagedRoles" border style="width: 100%; margin-top: 20px;">
      <el-table-column prop="roleId" label="角色编号" />
      <el-table-column prop="roleName" label="角色名称"  />
      <el-table-column prop="permissions" label="角色描述" min-width="250"/>
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button size="small" @click="handleEdit(row)">修改</el-button>
          <el-button size="small" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="totalRoles"
        :page-sizes="[5,10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 添加/编辑角色对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEditing ? '修改角色信息' : '添加角色'"
      width="50%"
      :before-close="handleCloseDialog"
    >
      <el-form :model="roleForm" label-width="100px">
        <el-form-item label="角色编号" v-if="isEditing">
          <el-input v-model="roleForm.roleId" disabled />
        </el-form-item>
        <el-form-item label="角色名称" required>
          <el-input v-model="roleForm.roleName" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input
            v-model="roleForm.permissions"
            type="textarea"
            :rows="4"
            placeholder="请输入角色描述"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="handleCloseDialog">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { addRoleTable, deleteRoleTable, getRoleTables, updateRoleTable } from '@/api/role'

// 分页参数
const currentPage = ref(1)
const pageSize = ref(10)

//表格需要展示的数据对象数组
const pagedRoles =ref([])

// 搜索参数
const searchParams = reactive({
  roleName: ''
})

// 计算总角色数（与adminmanagement一致）
const totalRoles = ref(0)

//查询当前表格中的数据
const queryRole =()=>{
  getRoleTables(searchParams).then((res)=>{
    if(res.code==2000){
      pagedRoles.value=res.data
      totalRoles.value=res.total
    }else{
      pagedRoles.value=[]
      totalRoles.value=0
    }
  })
}

queryRole()

//角色表单
const roleForm = reactive({
  roleId: '',
  roleName: '',
  permissions: ''
})

// 显示添加对话框
const showAddDialog = () => {
  isEditing.value = false
  resetForm()
  dialogVisible.value = true
}

// 显示编辑对话框
const handleEdit = (row) => {
  isEditing.value = true
  currentRoleId.value = row.roleId
  Object.assign(roleForm, JSON.parse(JSON.stringify(row)))
  dialogVisible.value = true
}

// 重置表单
const resetForm = () => {
  Object.assign(roleForm, {
    roleId: '',
    roleName: '',
    permissions: ''
  })
}

// 关闭对话框
const handleCloseDialog = () => {
  dialogVisible.value = false
}





// 对话框相关状态
const dialogVisible = ref(false)
const isEditing = ref(false)
const currentRoleId = ref('')


// 确认添加/编辑
const handleConfirm = () => {
  if (!roleForm.roleName) {
    ElMessage.warning('请填写角色名称')
    return
  }

  if (isEditing.value) {
    // 更新角色
    if (isEditing.value) {
      updateRoleTable(roleForm).then(res=>{
        if(res.code==2000){
          ElMessage.success('修改成功')
          handleSearch()
        }
      })
    }
  } else {
    // 检查编号是否已存在
    addRoleTable(roleForm).then(res=>{
      if(res.code==2000){
        ElMessage.success('添加成功')
        handleSearch()
      }
    })
  }
  dialogVisible.value = false
}

// 处理删除
const handleDelete = (row) => {
  ElMessageBox.confirm('是否确认删除该角色?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteRoleTable(row.roleId).then(res=>{
      ElMessage.success('修改成功')
      handleSearch()
    })
  })
}

// 处理搜索
const handleSearch = () => {
 queryRole()
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  queryRole()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  queryRole()
}

</script>

<style scoped>
.role-management {
  padding: 20px;
}

.operation-area {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.search-area {
  display: flex;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-button--text {
  padding: 0;
}
</style>